home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_02 / pugh / trysort.cpp < prev   
C/C++ Source or Header  |  1994-12-11  |  808b  |  38 lines

  1. #include <stdlib.h>
  2.  
  3. int compare_function(const void * a, const void * b);
  4.  
  5. class A
  6.     {
  7. private:
  8.     struct DataType
  9.           {
  10.           double x,y;
  11.           } * data;
  12.     int size;
  13. public:
  14.     A();
  15.     A(int n, double *x, double *y);
  16.     void sort();
  17.     };
  18.  
  19. void A::sort()
  20.     {
  21.     qsort (data, size, sizeof(DataType), compare_function);
  22.     }
  23.  
  24. int compare_function(const void * a, const void * b)
  25.     {
  26.     const DataType *first = (DataType*) a;
  27.     const DataType *second = (DataType*) b;
  28. //  Ought these declaration be as follows ?
  29. //  const A::DataType *first = (A::DataType*) a;
  30. //  const A::DataType *second = (A::DataType*) b;
  31.     if (first->x > second->x)
  32.       return 1;
  33.     else if (first->x < second->x)
  34.       return -1;
  35.     else
  36.       return 0;
  37.     }
  38.